﻿package net.wangit.adm.admin;

import java.util.List;

import net.wangit.context.ActionContext;
import net.wangit.jdbc.ColumnRow;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.Table;
import net.wangit.support.action.DefaultAction;
import net.wangit.user.admin.AdminCache;


public class DeleteAdminAction extends DefaultAction {

	private static final String ORGANIZATION_DELETE = "delete from AADMINORG where adminid=?";
	private static final String GROUP_DELETE = "delete from AADMINGROUP where adminid=?";
	private static final String ROLE_DELETE = "delete from AADMINROLE where adminid=?";
	private static final String USER_DELETE = "update AUSER set adminid='' where adminid=?";

	public DeleteAdminAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		super.doAction(db, ac);
		List tables = ac.getTables();
		Table table = (Table) tables.get(0);
		List rows = table.getRows();
		for (int i = 0; i < rows.size(); i++) {
			ColumnRow row = (ColumnRow) rows.get(i);
			if (row.isDeleteRow())
				delete(db, row.getPrimaryKey("adminid"));
		}

		AdminCache.clearAll();
	}

	private void delete(DB db, String oldId) throws Exception {
		db.execute("delete from AADMINORG where adminid=?", oldId, 2);
		db.execute("delete from AADMINGROUP where adminid=?", oldId, 2);
		db.execute("delete from AADMINROLE where adminid=?", oldId, 2);
		db.execute("update AUSER set adminid='' where adminid=?", oldId, 2);
	}
}
